package marmalius.command.admin;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import marmalius.Player;
import marmalius.command.BaseCommand;
import marmalius.player.Coordonne;
import marmalius.tools.DBService;
import marmalius.tools.LogService;

public class Tele extends BaseCommand {
	private Map<String, Coordonne> gameTele;
	
	public Tele() {
		gameTele = new HashMap<String, Coordonne>();
		loadGameTele();
	}
	
	public boolean loadGameTele() {
		gameTele.clear();
		int nb = 0;
		try {
			ResultSet rs = DBService.getInstance().doQuery("SELECT * FROM game_tele");
			while (rs.next()) {
				gameTele.put(rs.getString("name").toLowerCase(), new Coordonne(rs.getInt("x"), rs.getInt("y"), rs.getInt("h")));
				nb++;
			}
			rs.close();
			LogService.getInstance().logInfo("game_tele loaded ("+nb+")");
			return true;
		}
		catch (SQLException ex) {
			LogService.getInstance().logError("can't load game_tele");
			LogService.getInstance().logError(ex.getMessage());
		}
		return false;
	}

	public void process(Player player, String[] command) {
		if (command == null || (command.length != 1 && command.length != 3)) {
			showHelp(player);
			return;
		}
		
		if (command.length == 1) {
			Coordonne tele = gameTele.get(command[0].toLowerCase());
			if (tele != null)
				player.setTeleport(tele.getAbsX(), tele.getAbsY(), tele.getAbsH());
		}
		else
			player.setTeleport(ParseInteger(command[0]), ParseInteger(command[1]), ParseInteger(command[2]));
	}
	
}
